package com.kedacom.freedomLabel.view.fragment.setting;

import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kedacom.freedomLabel.AppExecutors;
import com.kedacom.freedomLabel.BR;
import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.databinding.FragmentBasicInformationBinding;
import com.kedacom.freedomLabel.databinding.ItemMachineBinding;
import com.kedacom.freedomLabel.databinding.ItemScaleRecordBinding;
import com.kedacom.freedomLabel.initdata.InitAll;
import com.kedacom.freedomLabel.initdata.bean.EventBusMessage;
import com.kedacom.freedomLabel.initdata.bean.MachineNumber;
import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.utils.log.LLog;
import com.kedacom.freedomLabel.view.activity.ScaleRecordActivity;
import com.kedacom.freedomLabel.view.fragment.SettingFragmentsControl;
import com.kedacom.freedomLabel.viewmodel.BasicInformationViewModel;
import com.kedacom.lego.adapter.recyclerview.LegoBaseRecyclerViewAdapter;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.OnMessage;
import com.kedacom.lego.annotation.ViewModel;
import com.seray.scaleviewlib.utils.LogUtils;
import com.seray.scaleviewlib.utils.Utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@ContentView(R.layout.fragment_basic_information)
@ViewModel(BasicInformationViewModel.class)
public class BasicInformationFragment extends ASettingFragment<FragmentBasicInformationBinding, BasicInformationViewModel> {


    LegoBaseRecyclerViewAdapter<MachineNumber> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        LLog.debug("BasicInformationFragment - onCreateView " + this.toString());
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LLog.debug("BasicInformationFragment - onDestroyView " + this.toString());
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LLog.debug("BasicInformationFragment - onViewCreated " + this.toString());
        mViewModel.mLiveDeviceCode.observe(this, config -> {
            if (config == null) {
                mBinding.basicSettingDeviceNumber.setContentText("");
            } else {
                mBinding.basicSettingDeviceNumber.setContentText(config.configValue);
            }
        });

        mBinding.basicSettingUser.setOnClickListener(v -> {
            beep();
            if (getActivity() instanceof SettingFragmentsControl.ChildSettingFragmentClickCallback) {
                ((SettingFragmentsControl.ChildSettingFragmentClickCallback) getActivity())
                        .onChildSettingFragmentItemClick(SettingFragmentsControl.Intention.OPEN_USER_INFORMATION);
            }
        });

//        mBinding.MachineRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
//        adapter = new LegoBaseRecyclerViewAdapter<MachineNumber>(R.layout.item_machine, new ArrayList<>(), BR.machine) {
//            @Override
//            protected void onCustomBindItem(ViewDataBinding binding, int position) {
//                super.onCustomBindItem(binding, position);
//                ((ItemMachineBinding) binding).index.setText(String.valueOf(position + 1));
//                ((ItemMachineBinding) binding).layout.setBackgroundColor(
//                        ContextCompat.getColor(getActivity(), position % 2 == 1 ? R.color.white_back : android.R.color.transparent)
//                );
//                ((ItemMachineBinding) binding).deleteMachine.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mViewModel.DeleteMachine(((ItemMachineBinding) binding).MachineName.getText().toString());
//                    }
//                });
//            }
//        };
//        mBinding.MachineRecycler.setAdapter(adapter);

//        mBinding.addMachine.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View v) {
////                mMisc.beep();
////                if(!TextUtils.isEmpty(mBinding.insertMachine.getText().toString())){
////                    MachineNumber machineNumber =new MachineNumber();
////                    machineNumber.setMachineName(mBinding.insertMachine.getText().toString());
////                    mViewModel.InsertMachine(machineNumber);
////                }else {
////                    showToast("机号不能为空");
////                }
////            }
////        });

//        mBinding.machineName.setText(getDeviceId());


//        String baseUrl = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("scales_post_url_post", "http://yzqa2q2g.ipyingshe.net");

//        mBinding.testInterface.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mMisc.beep();
//                InitAll.TestInterface01(baseUrl);
//            }
//        });

//        mViewModel.querAllMachine();
    }

//    @OnMessage("getMachineList")
//    private void getMachineListSuccess(List<MachineNumber> machineNumberList) {
//
//        adapter.setData(machineNumberList);
//    }

    @OnMessage(value = Options.MSG_CONNECT_SUCCESS_TEST, always = true)
    public void showPostSuccess(EventBusMessage msg) {

        if (msg.getSuccess() == 1) {
            showToast(msg.getDes(), TOAST_SUCCESS);
        } else {
            showToast(msg.getDes(), TOAST_ERROR);
        }

    }




    @Override
    public void saveData() {
        String deviceCode = mBinding.basicSettingDeviceNumber.getContentText();
        mViewModel.saveDeviceCode(deviceCode);
    }



    public String getDeviceId() {
        return Settings.System.getString(getActivity().getContentResolver(), Settings.System.ANDROID_ID);
    }

    @Override
    public String getFragmentTitle() {
        return Utils.getApp().getString(R.string.setting_frag_name_basic);
    }
}